<template>
  <div class="setting-container">
    <div v-loading="loading" class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="addPeople"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table :data="tableData">
              <el-table-column label="序号" type="index" :index="numIndex" />
              <el-table-column label="角色名称" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editOne(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 这里是分页器 -->
            <el-pagination
              :current-page="page"
              :page-sizes="[2, 5, 10, 20]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>

      </el-card>
      <el-dialog :title="showTitle" :visible.sync="dialogFormVisible" :close-on-click-modal="false" @click="closeDialog">
        <el-form ref="ruleForm" :model="form" :rules="rules">
          <el-form-item label="输入名称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="输入描述" :label-width="formLabelWidth" prop="description">
            <el-input v-model="form.description" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getPeopleAPI, deleteRoleApi, addPeopleAPI, getOneAPI, updateRoleApi } from '@/api/setting'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'second',
      tableData: [],
      page: 1, // 当前页
      pagesize: 5, // 每一页的总条数
      total: 0, // 总条数
      loading: false,
      dialogFormVisible: false,
      form: {
        name: '',
        description: ''
      },
      formLabelWidth: '120px',
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ] }
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.getPeople()
  },
  methods: {
    async editOne(id) {
      const { data: res } = await getOneAPI(id)
      // console.log(res)
      this.form = res
      this.dialogFormVisible = true
    },
    addPeople() {
      this.dialogFormVisible = true
    },
    closeDialog() {
      this.dialogFormVisible = false
      // this.$refs.roleForm.resetFields()
      this.form.name = ''
      this.form.description = ''
    },
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          await addPeopleAPI(this.form)
          // this.$refs.roleForm.resetFields()
          this.form.name = ''
          this.form.description = ''
          this.dialogFormVisible = false
          this.getPeople()
        } else {
          return false
        }
      })
    },
    handleClick(tab, event) {
      console.log(tab, event)
    },
    async getPeople() {
      if (this.form.id) {
        await updateRoleApi(this.form)
      } else {
        const { data: res } = await getPeopleAPI({
          page: this.page, pagesize: this.pagesize
        })
        this.tableData = res.rows
        this.total = res.total
      }
      this.closeDialog()
    },
    delRole(id) {
      this.$confirm('亲！您确定要删除吗？此操作不可逆哟', '温馨提示').then(async() => {
        await deleteRoleApi(id)
        this.$message.success('删除成功！')
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        this.getPeople()
      }).catch(() => {})
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.getPeople()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getPeople()
    },
    // 计算每一个数据的排序
    numIndex(index) {
      return this.pagesize * (this.page - 1) + index + 1
    }
  }
}
</script>

<style scoped>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 480px;
  }

</style>
